package com.wyiot.pet.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyiot.pet.model.entity.PetDO;
import com.wyiot.pet.model.vo.PetVO;
import com.wyiot.core.security.UserContext;
import com.wyiot.pet.service.PetService;
import com.wyiot.pet.model.dto.PetDTO;
import com.wyiot.core.api.ValidateGroup;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 用户宠物管理控制层
 *
 * @author yly
 * @since 2022/3/24
 */
@RestController
public class PetController {

    @Resource
    private PetService petService;

    /**
     * 查询当前用户的宠物列表
     *
     */
    @GetMapping("/pet/list")
    public Page<PetVO> queryPetList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
            , @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return petService.queryPetListByUserId(pageNum , pageSize, UserContext.getUserId());
    }

    /**
     * 根据宠物id查询宠物信息
     *
     * @param petId 宠物id
     */
    @GetMapping("/pet/{pId}")
    public PetDO queryPetById(@PathVariable("pId") Long petId) {
        return petService.getById(petId);
    }

    /**
     * 根据宠物id修改宠物信息
     *
     * @see PetDTO
     */
    @PutMapping("/pet")
    public void modifyPetInfo(@RequestBody @Validated({ValidateGroup.First.class}) PetDTO petDTO,BindingResult result) {
        PetDO petDO = new PetDO();
        BeanUtils.copyProperties(petDTO, petDO);
        petService.updateById(petDO);
    }

    /**
     * 根据宠物id删除宠物信息
     *
     * @param petId 宠物id
     */
    @DeleteMapping("/pet/{pId}")
    public void removePetById(@PathVariable("pId") Long petId) {
        petService.removeById(petId);
    }

    /**
     * 新增宠物
     *
     * @see PetDTO
     */
    @PostMapping("/pet")
    public void insertPet(@RequestBody @Validated({ValidateGroup.Second.class}) PetDTO petDTO,BindingResult result) {
        PetDO petDO = new PetDO();
        BeanUtils.copyProperties(petDTO, petDO);
        petDO.setUserId(UserContext.getUserId());
        petService.save(petDO);
    }

}
